package com.ztao.service.impl;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.ztao.pojo.ApAssociateWords;
import com.ztao.pojo.dto.UserSearchDto;
import com.ztao.service.ApAssociateWordsService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ApAssociateWordsServiceImpl implements ApAssociateWordsService {
    /**
     * 根据输入的词进行联想  -- 模糊查询
     *
     * @param userSearchDto
     * @return
     */
    @Resource
    private MongoTemplate mongoTemplate;
    @Override
    public ResponseResult findAssociate(UserSearchDto userSearchDto) {
        if(userSearchDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //执行模糊查询
        Query query=new Query(Criteria
                .where("associateWords")
                .regex(".*"+userSearchDto.getSearchWords()+".*"));
        List<ApAssociateWords> apAssociateWords = mongoTemplate.find(query, ApAssociateWords.class);
        return ResponseResult.okResult(apAssociateWords);
    }
}
